package com.gs.SynAntiWandering.presenter;


import com.google.gson.reflect.TypeToken;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.apiresult.LoginData;
import com.gs.SynAntiWandering.model.LoginModel;
import com.gs.SynAntiWandering.model.listener.OnResultListener;
import com.gs.SynAntiWandering.view.ILoginView;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginPresenter extends BasePresenter<ILoginView> {
    private LoginModel loginModel = new LoginModel();

    public void login() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userName", getView().getUserName());
            jsonObject.put("password", getView().getPassword());
            jsonObject.put("validateCode", "test");
            jsonObject.put("moduleId", 1);
            loginModel.login(jsonObject.toString(),
                    new OnResultListener() {
                        @Override
                        public void onSuccess(Object apiResult) {
                            if (getView() != null) {
                                getView().onLoginSuccess((ApiResultData<LoginData>) apiResult);
                            }
                        }

                        @Override
                        public void onFails(Exception e) {
                            if (getView() != null) {
                                getView().onLoginFail(e);
                            }
                        }
                    }, new TypeToken<ApiResultData<LoginData>>() {
                    }.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
